package com.mingtu.guards.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;

import com.mingtu.guards.R;
import com.mingtu.center.bean.PatrolPeopleBean;

import java.util.LinkedHashMap;
import java.util.List;


public class PopPeopleAdapter extends BaseAdapter implements View.OnClickListener {
    private Context context;
    private OnItemClickListener mOnItemClickListener = null;
    private List<PatrolPeopleBean.PageBean.ListBean> mData;
    private LinkedHashMap<Integer, Boolean> hashMap = new LinkedHashMap<>(); //存放是否选中的状态，默认为false
    private String selectedUserId = "";
    public PopPeopleAdapter(Context context, List<PatrolPeopleBean.PageBean.ListBean> mData) {
        this.context = context;
        this.mData = mData;
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i,false);
        }
    }

    @Override
    public int getCount() {
        return mData != null ? mData.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_filte, null);

            holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkbox);


            holder.checkbox.setOnClickListener(this);

            holder.checkbox.setTag(position);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        String text = mData.get(position).getName();
        holder.checkbox.setText(text);

        holder.checkbox.setChecked(hashMap.get(position));

        return convertView;
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    /**
     * 获取选中的userId
     */
    public String getSelectedUserId() {
        return selectedUserId;
    }



    /**
     * 更新数据
     * @param position
     */
    public void upData(int position){
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i,false);
        }
        hashMap.put(position,true);
        selectedUserId = mData.get(position).getUserId();
        notifyDataSetChanged();
    }

    /**
     * 重置选中状态
     */
    public void resetData(){
        for (int i = 0; i < mData.size(); i++) {
            hashMap.put(i,false);
        }
        selectedUserId = null;
        notifyDataSetChanged();
    }


    class ViewHolder {
        public CheckBox checkbox;
    }


    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

}
